package com.imooc.distributedemo.resume.leetcode;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * leetcode202 快乐数
 */
public class Solution202 {
    public static void main(String[] args) {
        int n = 19;
        boolean bool = isHappy(n);
        System.out.println(bool);
    }

    public static boolean isHappy(int n) {
        if(n==1){
            return true;
        }
        if(n==0){
            return false;
        }
//        // 递归
//        List<Integer> array = getAllLocation(n);
        int originalNumber = n; // 保存原始数字
        Set<Integer> seen = new HashSet<>();
        while (n!=1&&seen.contains(n)){
            seen.add(n);
            n = getNext(n);
        }

        return n==1;
    }

    private static int getNext(int n) {
        int totalSum = 0;
        while (n>0){
            int i = n %10;
            n /= 10;
            totalSum = i * i;
        }
        return totalSum;
    }

    private static List<Integer> getAllLocation(int n) {
        List<Integer> data = new ArrayList<>();
        while(n>0){
            int digit = n%10;
            data.add(digit);
            n /= 10;
        }
        return data;
    }
}
